<?php
declare (strict_types = 1);

namespace app\api\dao;

use app\api\server\Zt;
use think\facade\Db;

//订单
class OrderDao
{
    public $orderListModel;
    public $orderDetail;

    //新增订单
    public function add($productInfo,$userInfo,$param,$appid,$pay_type): array
    {
        $orderInfo=[
            "order_no"=>orderNo($userInfo["user_id"]),
            "order_name"=>$productInfo["name"],
            "product_name"=>$productInfo["name"],
            "product_code"=>$productInfo["product_code"],
            "price"=>$productInfo["price"],
            "user_id"=>$userInfo["user_id"],
            "open_id"=>$userInfo["open_id"],
            "phone"=>$userInfo["phone"],
            "channel"=>$userInfo["channel"],
            "pay_type"=>$pay_type,
            "channel_name"=>$userInfo["channel_name"],
            "source"=>$userInfo["source"],
            "param"=>json_encode($param,320),
            "appid"=>$appid,
            "create_time"=>date("Y-m-d H:i:s")
        ];
        $orderInfo["id"]=DB::table("zl_survey_order")->insertGetId($orderInfo);
        return $orderInfo;
    }

    //订单跟新
    public function update($order_no,$updateInfo)
    {
        return DB::table("zl_survey_order")
            ->where("order_no",$order_no)
            ->update($updateInfo);
    }

    //用户订单列表
    public  function list($user_id,$appid,$param): array
    {
        $this->orderListModel= DB::table("zl_survey_order")
            ->field("order_no,order_name,product_name,product_code,price,status,pay_status,pay_type,param,create_time,pay_time")
            ->where("user_id",$user_id)
            ->where("appid",$appid)
            ->order("id","desc");

        !empty($param) && $this->setWhere($param);
        return $this->orderListModel->paginate()->toArray();
    }

    //订单详情
    public  function detail($order_no,$user_id,$appid)
    {
        $this->orderDetail= DB::table("zl_survey_order")
            ->withoutField('id')
            ->where("order_no",$order_no)
            ->where("user_id",$user_id)
            ->where("appid",$appid)
            ->find();
        $this->orderDetail["report"]=DB::table("zl_survey_report")
            ->withoutField('id')
            ->where("order_no",$order_no)
            ->find();
        return $this->orderDetail;
    }

    //订单报告详情
    public  function report($order_no)
    {

        $data=DB::table("zl_survey_report")
            ->where("order_no",$order_no)
            ->find();

        if(empty($data["report_detail"]))
        {
            $report=(new Zt())->report($data["third_order_no"]);
            $data["report_detail"]=$report["data"];
        }
        return $data;
    }




    //订单查询
    public  function find($order_no)
    {
        $this->orderDetail= DB::table("zl_survey_order")
            ->where("order_no",$order_no)
            ->find();
        return $this->orderDetail;
    }


    //订单完成
    public  function finishPay($order_no)
    {
        DB::table("zl_survey_order")
            ->where("order_no",$order_no)
            ->update([
                "pay_status"=>20,
                "pay_time"=>date("Y-m-d H:i:s")
            ]);
    }


    /**
     * 设置查询条件
     * @param $where
     */
    public function setWhere($where)
    {
        if (isset($where['status']) && !empty($where['status'])) {

            $this->orderListModel->where('status', '=', trim($where['status']));
        }

        if (isset($where['pay_status']) && !empty($where['pay_status'])) {

            $this->orderListModel->where('pay_status', '=', trim($where['pay_status']));
        }


    }



}
